/**
 * @author Kenway Chung
 * Project 1 Compiler
 */
package edu.towson.cosc.cosc455.kchung2.project1;
import java.util.*;
import java.io.*;

public class Compiler {
	static String token;
	static MyLexicalAnalyzer lexer;
	static MySyntaxAnalyzer parser;
	static MySemanticAnalyzer semantic;
	static ArrayList<String> parseTree = new ArrayList<String>();
	static ArrayList<String> output = new ArrayList<String>();
	static ArrayList<String> variables = new ArrayList<String>();
	static ArrayList<String> values = new ArrayList<String>();
	static HashSet<String> valid = new HashSet<String>();
	static PrintStream myHTMLFile;
	public static void main(String []args) throws FileNotFoundException, IOException, CompilerException{
		token = "";
		File myFile = new File(args[0]);
		String fileName = myFile.getName();
		checkExt(fileName);
		String line = fileToString(myFile);
		//3. initialization
		valid.add("#START DOCUMENT");valid.add("#END DOCUMENT");valid.add("#START TITLE");valid.add("#END TITLE");
		valid.add("#START HEAD");valid.add("#END HEAD");valid.add("#START PARAGRAPH");valid.add("#END PARAGRAPH");
		valid.add("#START BOLD");valid.add("#END BOLD");valid.add("#START ITALICS");valid.add("#END ITALICS");
		valid.add("#START LIST");valid.add("#END LIST");valid.add("#START ITEM");valid.add("#END ITEM");
		valid.add("#NEWLINE ");valid.add("#START AUDIO");valid.add("#END AUDIO");valid.add("#START VIDEO");
		valid.add("#END VIDEO");valid.add("#START DEFINE");valid.add("#NAME ");valid.add("#VALUE ");
		valid.add("#END DEFINE");valid.add("#START USE");valid.add("#END USE");
		
		lexer = new MyLexicalAnalyzer(line, valid);
		parser = new MySyntaxAnalyzer(lexer);
		semantic = new MySemanticAnalyzer();
		parser.document();
		/*-----------lexer and parser will do its thing------------*/
		//6. call the semantic analyzer
		myHTMLFile = new PrintStream(new File(fileName.substring(0,fileName.length() - 4) + ".html"));
		output = semantic.convert(parseTree);
		//7. write the output file to html
		String html = treeToHTML(output);
		writeToHTMLFile(html, myHTMLFile);
		//8. clean up
	}
	private static String fileToString(File file) throws FileNotFoundException, IOException{
		StringBuilder sb = new StringBuilder();
		Scanner in = new Scanner(file);
		try{
			while(in.hasNextLine()){
				sb.append(in.nextLine());
			}
			return sb.toString();
		}
		finally{
			in.close();
		}
	}
	/**
	 * Converts the tree into a String to be output
	 * @param output
	 * @return
	 */
	private static String treeToHTML(ArrayList<String> output){
		String htmloutput = "";
		for(String s: output){
			htmloutput += s;
		}
		return htmloutput;
	}
	/**
	 * Writes the file into html.
	 * @param stuff
	 * @param go
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	private static void writeToHTMLFile(String stuff, PrintStream go) throws FileNotFoundException, IOException{
		Scanner in = new Scanner(stuff);
		while(in.hasNext()){
			go.print(in.next());
		}
		in.close();
	}
	/**
	 * Checks the extension of the file to make sure its .mkd
	 * @param fileName
	 */
	private static void checkExt(String fileName){
		if(!fileName.endsWith(".mkd")){
			System.out.println("Only .mkd files are accepted!");
			System.exit(0);
		}
	}
}
